package com.zx.ishopping.controller.common;

import com.zx.ishopping.DTO.UserLoginDTO;
import com.zx.ishopping.VO.LoginVo;
import com.zx.ishopping.VO.UserVo;
import com.zx.ishopping.results.Result;
import com.zx.ishopping.services.UserService;
import com.zx.ishopping.utils.AliOssUtil;
import com.zx.ishopping.utils.JsonUtil;
import com.zx.ishopping.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;


@Slf4j
@RestController("CommonController")
@RequestMapping("/common/user")
public class commonController {


    @Autowired
    private UserService userService;

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 用户登陆测试
     * @return User
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody UserLoginDTO userLoginDto){
        log.info("用户登陆开始{}",userLoginDto);
        LoginVo loginVo = userService.login(userLoginDto);
        return Result.success(loginVo);
    }

    /**
     * 文件上传
     * @param imageFile
     * @return
     */
    @PostMapping("/upload")
    public  Result<String> upload(MultipartFile imageFile){
        log.info("上传文件开始{}"+imageFile);
        try {
        String originalFilename = imageFile.getOriginalFilename();
        //获取原始文件名的后缀,拓展名
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString() + extension;
        String uploadFile = aliOssUtil.upload(imageFile.getBytes(), fileName);
        return Result.success(uploadFile);
        } catch (IOException e) {
            log.info("上传文件异常",e);
        }
        return Result.error("上传文件失败");
    }


    /**
     * 获取当前登录用户信息
     * @return
     */
    @GetMapping("/getUserInfo")
    public Result<UserVo> getAdminInfo(){
        long id = ThreadLocalUtil.get();
        log.info("用户id：{}",id);
        UserVo userVo = userService.getUserById(id);
        return Result.success(userVo);
    }
    
    @GetMapping("/getProvinces")
    public Result<List<Map<String, String>>> getProvinces(){
        try {
            List<Map<String, String>> provinces = JsonUtil.readAreaJson("provinces.json");
            return Result.success(provinces);
        }catch (IOException e){
            return Result.error("获取省份信息失败");
        }
        
        
    }

    /**
     * 根据省份编码获取城市信息
     * @param provinceCode
     * @return
     */
    @GetMapping("/getCities")
    public Result<List<Map<String, String>>> getCities(@RequestParam String provinceCode ) {
        try {
            List<Map<String, String>> allCities = JsonUtil.readAreaJson("cities.json");
            List<Map<String, String>> targetCities = allCities.stream()
                    .filter(city -> provinceCode.equals(city.get("provinceCode")))
                    .collect(Collectors.toList());
            return Result.success(targetCities);
        }catch (IOException e){
            return Result.error("获取城市信息失败");
        }
    }

    /**
     * 根据城市编码获取区县信息
     * @param cityCode
     * @return
     */
    @GetMapping("/counties")
    public Result<List<Map<String, String>>> getDistricts(@RequestParam String cityCode){
        try {
            List<Map<String, String>> allDistricts = JsonUtil.readAreaJson("areas.json");
            List<Map<String, String>> targetDistricts = allDistricts.stream()
                    .filter(district -> cityCode.equals(district.get("cityCode")))
                    .collect(Collectors.toList());
            return Result.success(targetDistricts);
        }catch (IOException e){
            return Result.error("获取区县信息失败"+e.getMessage());
        }
    }
}
